//
//  HMTabBar.swift
//  VVeibo21
//
//  Created by 李浩杰 on 16/10/22.
//  Copyright © 2016年 李浩杰. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //手写代码创建这个类，就必须实现该方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 添加控件
        addSubview(composeButton)
    }
    
    //布局视图
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = HMSCREENW * 0.2
        var index: CGFloat = 0
        for value in subviews {
            // 判断是否是我们想要的UITabBarButton
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                value.frame.size.width = w
                value.frame.origin.x = index * w
                index += 1
                //为中间按钮留下位置
                if index == 2 {
                    index += 1
                }
            }
        }
        //中间按钮的frame
        composeButton.center.x = HMSCREENW * 0.5
        composeButton.center.y = frame.size.height * 0.5
        
    }
    
    
    
    //懒加载中间按钮
    lazy var composeButton: UIButton = {
        
        let button = UIButton(setImgName: "tabbar_compose_icon_add", bacgroundImgName: "tabbar_compose_button", target: self, action: #selector(plusButtonclick))
    
        return button
    }()
    
    // MARK: - 监听方法
    //中间按钮点击事件
    @objc func plusButtonclick() {
        print("中间按钮点击事件")
    }
}
